let username = localStorage.username
let messageEl = document.querySelector('.message')
let contentBoxEl = document.querySelector('.contentBox')
let num = 0
void(async function getGoods() {
    let allGoods = await axios({
        method: 'post',
        url: 'http://127.0.0.1:9527/api/data/getUserFav',
        data: {
            "username": username
        }
    })
    let favData = allGoods.data.data
    if (favData[0] == null) {
        messageEl.style.display = "block"
    } else {
        messageEl.style.display = "none"
    }
    await Object.keys(favData).forEach((e) => {
        // console.log(favData[e]);
        let cartDes = favData[e].cartDes
        let cartImg = favData[e].cartImg
        let cartPrice = favData[e].cartPrice
        let cartName = favData[e].cartName

        let contentEl = document.createElement('div')
        contentEl.classList.add('content')
        contentEl.innerHTML = `
        <ul class="goodsBox">
        <div class="goodsImg">
            <img src="${cartImg}" alt="">
        </div>
        <div class="goodsText">
            <h3>${cartDes}</h3>
            <p></p>
            <p>￥${cartPrice}</p>
        </div>
        </ul>
        <div class="addBox">
            <span class="addBtn" data-name="${cartName}">删除</span>
        </div>
        `
        contentBoxEl.append(contentEl)

    })

    let addBtnList = document.querySelectorAll('.addBtn');
    let bgMsgEl = document.querySelector('.message')
    let contentBox = document.querySelector('.contentBox')
    let message = document.querySelector('.message')
    await addBtnList.forEach(function (el, index) {
        el.index = index;
        el.onclick = function () {
            
            let itemEl = this.closest('.content')
            let thisCartName = el.dataset.name
            console.log(thisCartName);
            let content = document.querySelectorAll('.content')
            let delGoods = axios({
                method: 'post',
                url: 'http://127.0.0.1:9527/api/data/delFav',
                data: {
                    "cartName": thisCartName
                }
            })
            itemEl.remove();
            if (content.length == 1) {
                message.style.display = "block"
            }
            
        }
    })
})()